package com.yjs.app.trade.service.service.util;

import java.io.IOException;

import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import com.yjs.app.trade.core.base.exception.BusinessException;
import com.yjs.app.trade.core.util.PropertiesUtils;
import com.yjs.app.trade.entity.base.SysConstant;

public class FastDFSUtil {

	private static TrackerClient trackerClient = null;
	private static TrackerServer trackerServer = null;
	private static StorageServer storageServer = null;
	private static StorageClient1 storageClient = null;
	
	private static String trackerHttpServer = null;
	
	private static String trackerHttpPort = null;
	
	private static Logger log = LoggerFactory.getLogger(Logger.class);
	
	static {
		String conf_Path = FastDFSUtil.class.getResource("/config_fdfs.properties").getFile();
		try {
			ClientGlobal.init(conf_Path);
			trackerClient = new TrackerClient();
			trackerServer = trackerClient.getConnection();
			storageServer = trackerClient.getStoreStorage(trackerServer);
			storageClient = new StorageClient1(trackerServer, storageServer);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("fastdfs连接出错:" + e);
		}
	}
	
	private static StorageClient1 getStorageClient () {
		if (storageClient != null) {
			return storageClient;
		} else {
			try {
				Thread.sleep(1000 * 60 * 60 * 24);
			} catch (Exception e) {
				e.printStackTrace();
			}
			init();
			return getStorageClient();
		}
	}
	
	private static void init () {
		String conf_Path = FastDFSUtil.class.getResource("/config_fdfs.properties").getFile();
		try {
			ClientGlobal.init(conf_Path);
			trackerClient = new TrackerClient();
			trackerServer = trackerClient.getConnection();
			storageServer = trackerClient.getStoreStorage(trackerServer);
			storageClient = new StorageClient1(trackerServer, storageServer);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("fastdfs连接出错:" + e);
		}
	}
	
	public static String uploadFile (MultipartFile file) {
		long startTime = System.currentTimeMillis();
		String[] uploadResults = null;
		String fName = file.getOriginalFilename();
		int index = fName.lastIndexOf(".");
		String extName = "";
		if (index >= 0 && index < fName.length() - 1){
			extName = fName.substring(index+1);
			extName = extName.toLowerCase();
			fName = fName.substring(0, index);
		}else{
			throw new BusinessException(SysConstant.ErrorCode.SYS_ERROR, "文件名格式非法"); 	
		}
		try {
			storageClient = getStorageClient(); 
			uploadResults = storageClient.upload_file(file.getBytes(), extName, null);
		} catch (IOException e) {
			log.error("IO Exception when uploadind the file: " + fName, e);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("Non IO Exception when uploadind the file: " + fName, e);
		}
		log.debug("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");

		if (uploadResults == null) {
			log.error("upload file fail, error code: " + storageClient.getErrorCode());
		}
		return "http://" + getFastDFSAccessServer() + ":" + getFastDFSAccessport() + "/" + uploadResults[0] + "/" + uploadResults[1];
	}
	
	public static String getFastDFSAccessServer () {
		if (trackerHttpServer != null) {
			return trackerHttpServer;
		}
		trackerHttpServer = PropertiesUtils.getOtherProperty("config_fdfs.properties", "http.tracker_http_server");
		return trackerHttpServer;
	}
	
	public static String getFastDFSAccessport () {
		if (trackerHttpPort != null) {
			return trackerHttpPort;
		}
		trackerHttpPort = PropertiesUtils.getOtherProperty("config_fdfs.properties", "http.tracker_http_port");
		return trackerHttpPort;
	}
}
